package com.jt.controller;


import com.jt.entity.Classs;
import com.jt.entity.Page;
import com.jt.entity.Subject;
import com.jt.service.ClasssService;
import com.jt.service.SubjectService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;
    @Autowired
    private ClasssService classsService;


    @RequestMapping("/queryPage")
    public String query(ModelMap modelMap, HttpServletRequest request, Page page){
        String currentPage=request.getParameter("currentPage");
        System.out.println(currentPage);
        System.out.println(page.getCurrentPage());
        Integer curr=1;
        if (currentPage!=null){
            curr=Integer.parseInt(currentPage);
            System.out.println(curr);
        }
        page.setCurrentPage(curr);
        page.setCount(subjectService.count());
        page.setUrl("/subject/queryPage?");
        RowBounds rowBound=new RowBounds((page.getCurrentPage()-1)*page.getSize(),page.getSize());
        List<Subject> subject = subjectService.query(rowBound);
        List<Classs> classses = classsService.queryAll();
        modelMap.addAttribute("classses",classses);
        modelMap.addAttribute("subject",subject);
        modelMap.addAttribute("page",page);
        System.out.println(subject);
        System.out.println(page);
        System.out.println(classses);
        return "subject/subject_list";
    }


 @RequestMapping("/add")
  public String add(ModelMap modelMap){
     System.out.println("add");
     List<Classs> classses = classsService.queryAll();
     modelMap.addAttribute("classses",classses);

     return "/subject/subject_add";
}
    @RequestMapping("/querybyid")
    public  String querybyclassid(Integer id,ModelMap modelMap){

        Subject subject = subjectService.querybyid(id);
        System.out.println("update"+subject);
        modelMap.addAttribute("subject",subject);
        return "subject/subject_update";
    }
     @RequestMapping("/delete")
     public  String delete(Integer id){
         Integer delete = subjectService.delete(id, 1);
         return "/subject/queryPage";
     }
}
